package com.thunisoft.netty.custom;

import java.util.UUID;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @title NettyCustomClient.java
 * @package com.thunisoft.netty.custom
 * @projectName spring.boot
 * @author yzj 
 * @date 2019年10月8日 下午5:56:39
 */
public class NettyCustomClient {
	public static void main(String[] args) throws InterruptedException {
		 EventLoopGroup group = new NioEventLoopGroup();
	        try {
	            Bootstrap b = new Bootstrap();
	            b.group(group).channel(NioSocketChannel.class)
	                    .handler(new NettyCustomClientInitializer());

	            // Start the connection attempt.
	            Channel ch = b.connect("127.0.0.1", 8888).sync().channel();

	            int version = 1;
	            String sessionId = UUID.randomUUID().toString();
	            String content = "I'm the luck protocol!";

	            CustomHeader header = new CustomHeader(version, content.length(), sessionId);
	            CustomMessage message = new CustomMessage(header, content);
	            ch.writeAndFlush(message);

	            ch.close();

	        } finally {
	            group.shutdownGracefully();
	        }
	}

}
